package ebj.common.queue.request.service;

import java.io.IOException;
import java.io.OutputStream;

import org.apache.log4j.Logger;

import com.google.gson.Gson;
import com.sun.net.httpserver.Headers;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;

import ebj.common.queue.meta.Message;

public class TestOk implements HttpHandler{
    private static Logger logger = Logger.getLogger(TestOk.class);
    // final static String mimeType = "text/json";
    // final static String mimeType = "text/csv";
    final static String mimeType = "file";
    
    public void handle(HttpExchange httpExchange) throws IOException {
        String query = httpExchange.getRequestURI().getQuery();
        logger.info("query: " + query);
        System.out.println(query);
        // Message message = new Message(MSGCode.NDP_MSG_TYPE_INFO, MSGCode.NDP_MSG_CODE_SERVER_OK, MSGCode.NDP_MSG_SERVER_OK);
        // String response = XMLUtils.msg2XML(message);
        String response = new Gson().toJson(new Message("info", "200", "Server is running."));
        
        // 响应包含文件下载时，设置下面2行
        Headers responseHeaders = httpExchange.getResponseHeaders();
        responseHeaders.set("Content-Type", mimeType);
        responseHeaders.set("Content-Disposition","attachment; filename=\""+ new String(("filename_prefix" + "-" + "filename_suffix" + ".csv").getBytes("utf-8"),"utf-8") + "\"");
        
        httpExchange.sendResponseHeaders(200, response.length());
        OutputStream outputStream = httpExchange.getResponseBody();
        outputStream.write(response.getBytes("UTF-8"));
        outputStream.flush();
        outputStream.close();
    }

}
